//
//  NSString+Addition.m
//  BaDouXueBa
//
//  Created by ChEngZhi on 15/7/21.
//  Copyright (c) 2015年 BaiShiXing. All rights reserved.
//

#import "NSString+Addition.h"

@implementation NSString (Addition)

#pragma mark 判断该字符串是否满足密码要求 （密码由8~20位半角字符（数字、字母、符号），至少2种组成，区分大小写）
- (BOOL)isPasswordString{
    
    NSString * curString = self;
    // 是纯字母
    if ([curString isAllLetters]) {
        return NO;
    }
    // 是纯数字
    if ([curString isAllNumbers]) {
        return NO;
    }
    // 是纯字符
    if ([curString isAllSymbols]) {
        return NO;
    }
    return YES;
}

#pragma mark 纯字符
- (BOOL)isAllSymbols{
    
    NSCharacterSet * symbolSet = [NSCharacterSet characterSetWithCharactersInString:@"@／,.?。，？：；（）¥「」＂、[]{}#%-*+=_\\|~＜＞<>$?^?'@#$%^&*()_+'\""];
    NSString * curString = self;
    curString = [curString stringByTrimmingCharactersInSet:symbolSet];
    if(curString.length > 0)
    {
        return NO;
    }
    return YES;
}

#pragma mark 纯数字
- (BOOL)isAllNumbers{
    
    NSString * curString = self;
    curString = [curString stringByTrimmingCharactersInSet:[NSCharacterSet decimalDigitCharacterSet]];
    if(curString.length > 0)
    {
        return NO;
    }
    return YES;
}

#pragma mark 纯字母
- (BOOL)isAllLetters{
    
    NSString * curString = self;
    curString = [curString stringByTrimmingCharactersInSet:[NSCharacterSet letterCharacterSet]];
    
    if(curString.length > 0) {
        return NO;
    }
    return YES;
}

#pragma mark 判断该字符是否包含另一个字符串
- (BOOL)isIncludeString:(NSString *)otherString {
    
    if ([self rangeOfString:otherString].location != NSNotFound) {
        return YES;
    }
    return NO;
}

#pragma mark 判断单个字符是不是字母
- (BOOL) isLetter {
    
    int indexAsciiCode = [self characterAtIndex:0];
    // 大写字母
    if ((indexAsciiCode > 64) && (indexAsciiCode < 91)) {
        return YES;
    }
    // 小写字母
    if ((indexAsciiCode > 96) && (indexAsciiCode < 123)) {
        return YES;
    }
    return NO;
}

- (BOOL)isCanUsed {
    
    if (!self || [self isEqualToString:@""]) {
        return NO;
    }
    return YES;
}

#pragma mark 根据当前的时间戳字符串获取一个日期字符串
- (NSString *)getDateString {
//    NSString *str = @"1368082020";  // 时间戳
    
    NSTimeInterval time = [self doubleValue]; // 不加时差
    NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:time];
    
    //实例化一个NSDateFormatter对象
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //设定时间格式,这里可以设置成自己需要的格式
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    NSString * theDateStr = [dateFormatter stringFromDate: detaildate];
    return theDateStr;

}

///// 判断该字符串是不是有效的邮箱号码
//检测是否是邮箱号码
- (BOOL) isValidateEmail {
    
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:self];
}

// 获取文件大小
- (long long)fileSize {
    // 1.文件管理者
    NSFileManager *mgr = [NSFileManager defaultManager];
    
    // 2.判断file是否存在
    BOOL isDirectory = NO;
    BOOL fileExists = [mgr fileExistsAtPath:self isDirectory:&isDirectory];
    // 文件\文件夹不存在
    if (fileExists == NO) return 0;
    
    // 3.判断file是否为文件夹
    if (isDirectory) { // 是文件夹
        NSArray *subpaths = [mgr contentsOfDirectoryAtPath:self error:nil];
        long long totalSize = 0;
        for (NSString *subpath in subpaths) {
            NSString *fullSubpath = [self stringByAppendingPathComponent:subpath];
            totalSize += [fullSubpath fileSize];
        }
        return totalSize;
    } else { // 不是文件夹, 文件
        // 直接计算当前文件的尺寸
        NSDictionary *attr = [mgr attributesOfItemAtPath:self error:nil];
        return [attr[NSFileSize] longLongValue];
    }
}

@end
